package my.hnb.android.model;


import java.util.ArrayList;

import my.hnb.android.common.ActionEvent;
import my.hnb.android.common.ModelEvent;
import my.hnb.android.constant.ErrorConstants;
import my.hnb.android.constant.IntentConstants;
import my.hnb.android.controller.UserController;
import my.hnb.android.dto.SongDTO;
import my.hnb.android.dto.SongDetailDTO;
import my.hnb.android.network.HTTPMessage;
import my.hnb.android.network.HTTPResponse;
import android.os.Bundle;

public class UserModel extends AbstractModelService {
	static UserModel instance;

	protected UserModel() {
	}

	public static UserModel getInstance() {
		if (instance == null) {
			instance = new UserModel();
		}
		return instance;
	}
	
	
	@Override
	public void onReceiveData(HTTPMessage mes) {
		ActionEvent actionEvent = (ActionEvent) mes.getUserData();
		ModelEvent model = new ModelEvent();
		model.setDataText(mes.getDataText());
		model.setParams(((HTTPResponse) mes).getRequest().getDataText());
		model.setActionEvent(actionEvent);
		
//		if (StringUtil.isNullOrEmpty((String) mes.getDataText())) {
//			model.setModelCode(ErrorConstants.ERROR_COMMON);
//			UserController.getInstance().handleErrorModelEvent(model);
//			return;
//		}
		switch (mes.getAction()) {
		default:
			break;
		}
	}
	
	
	
	
	@Override
	public void onReceiveError(HTTPResponse mes) {
		// TODO Auto-generated method stub
		ActionEvent actionEvent = (ActionEvent) mes.getUserData();
		ModelEvent model = new ModelEvent();
		model.setDataText(mes.getDataText());
		model.setParams(((HTTPResponse) mes).getRequest().getDataText());
		model.setActionEvent(actionEvent);

//		if (StringUtil.isNullOrEmpty((String) mes.getDataText())) {
//			model.setModelCode(ErrorConstants.ERROR_COMMON);
//			UserController.getInstance().handleErrorModelEvent(model);
//			return;
//		}
		switch (mes.getAction()) {
		}
	}

	/**
	 * Lay danh sach bai nhac
	 * @author BANGHN
	 */
	public void requestGetListSong(ActionEvent e){
		ModelEvent model = new ModelEvent();
		model.setActionEvent(e);
		Bundle bundle = (Bundle) e.viewData;
		try {
			String searchInfo = bundle.getString(IntentConstants.INTENT_SONG_INFO);
			int pageNumber = bundle.getInt(IntentConstants.INTENT_PAGE_NUMBER);
			int favorite = bundle.getInt(IntentConstants.INTENT_SONG_FAVORITE);
			int searchOption = bundle.getInt(IntentConstants.INTENT_SEARCH_OPTION);
			try {
				ArrayList<SongDTO> result = SQLUtils.getInstance().getSongs(searchInfo, pageNumber, searchOption,
						favorite);

				if (result != null) {
					model.setModelData(result);
					model.setModelCode(ErrorConstants.ERROR_CODE_SUCCESS);
					UserController.getInstance().handleModelEvent(model);
				} else {
					model.setModelCode(ErrorConstants.ERROR_COMMON);
					UserController.getInstance().handleErrorModelEvent(model);
				}
			} catch (Exception ex) {
				model.setModelCode(ErrorConstants.ERROR_COMMON);
				model.setModelMessage(ex.getMessage());
				UserController.getInstance().handleErrorModelEvent(model);
			}
		} catch (Exception ex) {
			model.setModelCode(ErrorConstants.ERROR_COMMON);
			model.setModelMessage(ex.getMessage());
			UserController.getInstance().handleErrorModelEvent(model);
		} finally {
		}
	}

	/**
	 * get thong tin bai hat chi tiet
	 * @author BANGHN
	 * @param e
	 */
	public void requestGetSongDetail(ActionEvent e) {
		ModelEvent model = new ModelEvent();
		model.setActionEvent(e);
		Bundle bundle = (Bundle) e.viewData;
		try {
			String songCode = bundle.getString(IntentConstants.INTENT_SONG_CODE);
			try {
				SongDetailDTO result = SQLUtils.getInstance().getSongDetail(songCode);

				if (result != null) {
					model.setModelData(result);
					model.setModelCode(ErrorConstants.ERROR_CODE_SUCCESS);
					UserController.getInstance().handleModelEvent(model);
				} else {
					model.setModelCode(ErrorConstants.ERROR_COMMON);
					UserController.getInstance().handleErrorModelEvent(model);
				}
			} catch (Exception ex) {
				model.setModelCode(ErrorConstants.ERROR_COMMON);
				model.setModelMessage(ex.getMessage());
				UserController.getInstance().handleErrorModelEvent(model);
			}
		} catch (Exception ex) {
			model.setModelCode(ErrorConstants.ERROR_COMMON);
			model.setModelMessage(ex.getMessage());
			UserController.getInstance().handleErrorModelEvent(model);
		} finally {
		}
	}
	
	
	
	/**
	 * get thong tin bai hat chi tiet
	 * @author BANGHN
	 * @param e
	 */
	public void requestUpdateFavorite(ActionEvent e) {
		ModelEvent model = new ModelEvent();
		model.setActionEvent(e);
		Bundle bundle = (Bundle) e.viewData;
		try {
			String songCode = bundle.getString(IntentConstants.INTENT_SONG_CODE);
			int favorite = bundle.getInt(IntentConstants.INTENT_SONG_FAVORITE);
			try {
				int result = SQLUtils.getInstance().updateSongFavorite(songCode, favorite);
				
				if (result > 0) {
					model.setModelData(result);
					model.setModelCode(ErrorConstants.ERROR_CODE_SUCCESS);
					UserController.getInstance().handleModelEvent(model);
				} else {
					model.setModelCode(ErrorConstants.ERROR_COMMON);
					UserController.getInstance().handleErrorModelEvent(model);
				}
			} catch (Exception ex) {
				model.setModelCode(ErrorConstants.ERROR_COMMON);
				model.setModelMessage(ex.getMessage());
				UserController.getInstance().handleErrorModelEvent(model);
			}
		} catch (Exception ex) {
			model.setModelCode(ErrorConstants.ERROR_COMMON);
			model.setModelMessage(ex.getMessage());
			UserController.getInstance().handleErrorModelEvent(model);
		} finally {
		}
	}
	
}
